Uec3.ShowSaleDetailsWindow = Ext.extend(Ext.Window, {
	initPanel : function() {
		
		this.querySaleDetailsListUrl = RES_PATH + '/saleOrder/listOnlineDetails.do';
		
		this.createAllComb();
		
//		this.statusStore = this.createStatusStore();
		this.advancedQueryPanel = this.createAdvanceQueryPanel();
		this.openWindow = this.createOpenWindow();
		
		this.record = this.createRecord();
		this.store = this.createStore();
		this.colName = this.createColName();
		this.grid = this.createGrid();
		
		this.joinPanel = this.createJoinPanel();
		
		this.add(this.joinPanel);
		this.doLayout();
		this.show();
		this.creator.app.getDesktop().addAutoRun(this.getId());

		this.store.load();
		
//		this.captureStoreEvent();
		
	},
	moduleActionHtml : function(moduleId, action, value) {
		if(this.store.getById(value).data.workFlowId == '2') {
			action.desc = '处理订单';
		} else {
			action.desc = '查看单据';
		}
		return "<a style='font-size:12px' href=javascript:Uec3.App.getDesktop().getWindow('showSaleDetailsWindow')." + action.func + "(" + value + "); >" + action.desc + "</a>";
	},
	moduleActionRenderer : function(actions) {
		var ref = this;
		if (Ext.isArray(actions)) {
			return function(value, cellmeta, record, rowIndex, columnIndex, store) {
				var html = "";
				for (var i = 0; i < actions.length; i++) {
					var v = value;
					if (actions[i].reader) {
						v = actions[i].reader(value, cellmeta, record, rowIndex, columnIndex, store);
					}
					html += ref.moduleActionHtml(ref.creator.id, actions[i], v) + "&nbsp;&nbsp;";
				}
				return html;
			};
		} else {
			return function(value, cellmeta, record, rowIndex, columnIndex, store) {
				var v = value;
				if (actions.reader) {
					v = actions.reader(value, cellmeta, record, rowIndex, columnIndex, store);
				}
				return ref.moduleActionHtml(ref.creator.id, actions, v);
			};
		}
	},
	toOpenSaleOrder : function(id) {
		var rec = this.store.getById(id);
		var desktop = Uec3.App.getDesktop();
		if(rec.data.workFlowId + '' == '2') {
			
			var compId = rec.data.buyerId;
			
			desktop.launchWindowByParam("addAutoSaleOrderWindowCreator", {init_sellerCode : rec.data.codeOfSeller, id : rec.data.ordersId});
			
			/*Ext.Ajax.request({
				url : RES_PATH + '/partner/checkPartner.do',
				params : {'companyId' : compId, 'relation' : '0'},
				success : function(data) {
					var bo = Ext.decode(data.responseText);
					if(bo && bo.result + '' == 'go_on') {
//						var desktop = Uec3.App.getDesktop();
						desktop.launchWindowByParam("addAutoSaleOrderWindowCreator", {init_sellerCode : rec.data.codeOfSeller, id : rec.data.ordersId});
					} else {
						
						
						
						Ext.Msg.confirm("提示", '打开订单先要加对方为客户，确定添加吗？', function(btnId) {
							if(btnId == 'no') {
								return;
							} else {
								Ext.Ajax.request({
									url : RES_PATH + '/partner/addPartner.do',
									params : {'companyId' : compId, 'relation' : '0'},
									success : function(data) {
										var bo = Ext.decode(data.responseText);
										if(bo && bo.result + '' == 'ok') {
											Ext.Msg.confirm("提示", '客户添加成功,是否打开订单？', function(btnId) {
												if(btnId == 'no') {
													return;
												} else {
//													var desktop = Uec3.App.getDesktop();
													desktop.launchWindowByParam("addAutoSaleOrderWindowCreator", {init_sellerCode : rec.data.codeOfSeller, id : rec.data.ordersId});
												}
											});
										} else {
											Ext.Msg.alert("提示", '客户添加失败..');
										}
									}
								});
							}
						});
					}
				}
			});*/
			
			/*desktop.launchWindowByParam("addAutoSaleOrderWindowCreator", {
//				init_status : rec.data.status,
				id : rec.data.ordersId
			});*/
			
		} else if(rec.data.workFlowId + '' == '3') {
			desktop.launchWindowByParam("addOutSaleOrderWindowCreator", {
				init_sellerCode : rec.data.codeOfSeller,
				id : rec.data.ordersId
			});
		}
	},
//	captureStoreEvent : function() {
//		Ext.util.Observable.capture(this.store, function(e) {
//			if(e == 'load') {
//				var records = this.store.getRange();
//				var ref = this;
//				Ext.each(records, function(record) {
//					var obj = Ext.get(SALE_OPEN_STRING + record.id + this.versionCode);
//					record.main_win = ref;
//					obj.on('click', function() {
//						this.main_win.toOpenSaleOrder(this);
//					}.createDelegate(record))
//				}.createDelegate(this));
//				
//			}
//		}.createDelegate(this));
//	},
	createAllComb : function() {
		// 来源下拉框
		var sourceType_store = getDictionaryStore(RES_PATH, {
			group : 'part_source'
		});
		this.source_cmb = getDictionaryComboBox(sourceType_store, 'local', 'part_source',
				'来源', 'source');
		sourceType_store.load();
	},
//	createStatusStore : function() {
//		return new Ext.data.JsonStore({
//           url:Uec3.App.Constants.homeUrl + "/dictionary/findByVarGroup.do?group=order_status",
//           root: "",
//           autoLoad:true,
//           fields:[{name:'varIndex',type:'int'},{name:'varName',type:'string'}]
//        }); 
//	},
	createOpenWindow : function() {
		return new Ext.Window({
			title:'高级查找',
		    layout:'fit',
	    	width:380,
	    	closeAction:'hide',
	    	height:330,
			resizable :true,
			shadow : true,
			modal :true,
	    	closable:true,
	    	bodyStyle:'padding:5 5 5 5',
	    	animCollapse:true,
			items:[this.advancedQueryPanel]
	    });
	},
	createAdvanceQueryPanel : function() {
		var ds_pur_status_select = getDictionaryStore(RES_PATH, {
			group : 'order_status'
		});
		
		this.status_cmb = getDictionaryComboBox(ds_pur_status_select, 'remote', 'status',
				'单据状态', 'status', 200);
				
		return new Ext.form.FormPanel({
		         labelSeparator : "：",
		         frame:true,
		         border:false,			    
		         items:[
		            {
		              xtype:'textfield',
				      width : 200,
		              fieldLabel:'单据号',
		              name:'codeOfSeller'
		            }, this.status_cmb ,{
		               xtype:'textfield',
				       width : 200,
		               fieldLabel:'供货商',
		               name:'partnerIdByBuyer' 
		            },{
		               xtype:'datefield',
				       width : 200,
		               fieldLabel:'开始时间',
		               name:'gmtStart' ,
		               format:'Y-m-d'
		            },{
		               xtype:'datefield',
		               width : 200,
		               fieldLabel:'结束时间',
		               name:'gmtEnd',
		               format:'Y-m-d'
		            },{
		               xtype:'panel',
		               layout:'hbox',
		               border: false,
		               ctCls:'x-form-item',
		               items:[
		                 {
		                   xtype:'label',
		                   text:'进价(金额)：',
		                   margins:'4 0'
		                  // style:'font-size:15px'
		                   //ctCls:'x-form-item-label'
		                 },{
		                   xtype:'textfield',
		                   name:'minAmount',
		                   width:90,
		                   margins:'0 0 0 35'
		                 },{
		                    xtype:'label',
		                    text:'——'
		                 },{
		                    xtype:'textfield',
		                    name:'maxAmount', 
		                    width:90
		                 }
		              ]
		              
		            }
		         ],
		      buttons:[
		        { 
		          text:'查找',
				  handler : this.searchResultByCondition.createDelegate(this)
		        },{
		     	  text:'关闭',
		     	  handler : function(){	
		     	    this.openWindow.hide();
		     	  }.createDelegate(this)
		        }
		      ]
			
		});
	},
	searchResultByCondition : function() {
		var form_values = this.advancedQueryPanel.getForm().getValues();
	
		for (var j in form_values) {
			form_values[j] = castNoSelToNULL(form_values[j]);
		}
			
		form_values = Ext.util.JSON.encode(form_values);
		if(form_values) {
			this.store.baseParams = {'conditions' : form_values};
		} else {
			this.store.baseParams = {};
		}
		this.store.load();
		this.openWindow.hide();
	},
	createRecord : function() {
		return Ext.data.Record.create( [ 
	       		{
	       			name : 'id'
	       		},{
	       			name : 'code'
	       		}, {
	       			name : 'name'
	       		}, {
	       			name : 'source'
	       		}, {
	       			name : 'measurementUnit'
	       		}, {
	       			name : 'brand'
	       		}, {
	       			name : 'origin'
	       		}, {
	       			name : 'price'
	       		}, {
	       			name : 'codeOfSeller'
	       		}, {
	       			name : 'orderQuantity'
	       		}, {
	       			name : 'level1Id'
	       		}, {
	       			name : 'partId'
	       		}, {
	       			name : 'level1Name'
	       		}, {
	       			name : 'level2Name'
	       		}, {
	       			name : 'ordersId'
	       		},{
	       			name : 'status'
	       		}, {
	       			name : 'sellerId'
	       		}, {
	       			name : 'workFlowId'
	       		}, {
	       			name : 'buyerId'
	       		}
	    ] );
	},
	createStore : function() {
		return new Ext.data.Store( {
			proxy : new Ext.data.HttpProxy(
				{
					url : this.querySaleDetailsListUrl,
					method : 'post'
				}
			),
			pruneModifiedRecords : true, 
			paramNames : {
//				start : "start",
//				limit : "limit"
	//			sort : "sort",
	//			dir : "dir"   
			},
			remoteSort:true,
			reader : new Ext.data.JsonReader(
				{
					totalProperty : 'totalProperty',
					root : 'root'
				}, 
				this.record
			)
					
		} );
	},
	createColName : function() {
		
//		function createAddExecBtn(value, metadata, record, 
//			rowIndex, colIndex, store) {
//	
//			return '<div><a id="' + SALE_OPEN_STRING + record.id + this.versionCode + '" href="#">打开</a></div>';
//		}
		
		return new Ext.grid.ColumnModel( [ new Ext.grid.RowNumberer(),
			{
				header : '操    作',
				dataIndex : 'id',
				renderer : this.moduleActionRenderer({
					func : "toOpenSaleOrder"
//					desc : "查看单据"
				})
			}, {
				header : '单据号',
				dataIndex : 'codeOfSeller'
			}, {
				header : '零件编码',
				dataIndex : 'code'
			}, {
				header : '零件名称',
				dataIndex : 'name'
			}, {
				header : '来源',
				dataIndex : 'source',
				renderer : Ext.util.Format.comboRenderer(this.source_cmb)
//				editor : this.source_cmb
			}, {
				header : '单位',
				dataIndex : 'measurementUnit'
			}, {
				header : '订购数量',
				dataIndex : 'orderQuantity'
			}, {
				header : '订购价',
				dataIndex : 'price'
			}, {
				header : '品牌',
				dataIndex : 'brand',
				sortable : true
			}, {
				header : '产地',
				dataIndex : 'origin',
				sortable : true
			}
		] );
	},
	createGrid : function() {
		return new Ext.grid.GridPanel( {
			title : '销售订单明细',
//			id : SHOW_DETAILS_GRID_ID,
			cm : this.colName,
			store : this.store,
			iconCls : 'icon-grid',
			loadMask : {
				msg : '数据加载中...'
			},
			viewConfig : {
				forceFit:true
			},
//			width : '100%',
			height : 380,
	//		autoHeight : true,
			bbar: new Ext.PagingToolbar( {
	            pageSize : purOrSaleOrderPageSize,
	        	store : this.store,
	       	 	displayInfo : true,
	        	displayMsg : '第 {0} - {1} 条  共 {2} 条',
	        	emptyMsg : "没有记录"
	        	
	        } )
		} );
	},
	createJoinPanel : function() {
		return new Ext.FormPanel( {
	//	      title:'浏览订单',
//	          applyTo : document.body,
		      items:[
		      	{
		          height:60,
		          layout:'hbox',
		          border:false,
		          layoutConfig: 
		             {     
		               padding:'5',
					   align:'middle' 
                  },
		          items : [
					 {				    
						xtype : 'label',
					    text : '浏览销售订单明细',
					    style : 'color:#000;font-size:26px;background:#fff'
					 }, {
		                xtype:'spacer',
                        flex:0.5
		             }, {
		             	xtype : 'button',
		             	text : '切换到单头浏览方式',
		             	margins:'0',
		             	handler : this.showSaleByMode.createDelegate(this)
		             }, {
	                     xtype : 'numberfield',
	                     name : '_codeOfSeller'
//	                     id : '_codeOfSeller'
	                 }, {
		                  xtype : 'button',
		                  text : '查找单据号',                   
		                  margins : '0 10',
		                  handler : this.searchByCode.createDelegate(this)
		             }, {
		                  xtype : 'button',
		                  text : '高级查找', 
		                  handler : this.queryByComplex.createDelegate(this)
		             }, {
		                  xtype : 'spacer',
		                  flex : 1
		             }, {
		                  xtype : 'button',
		                  text : '新增销售订单',
		                  margins:'0 50',
		                  handler : this.newSaleOrder.createDelegate(this)
		             }
		         ]
		      }, 
		      	this.grid
		    ]
		} );
	},
	showSaleByMode : function() {
		this.creator.app.getDesktop().launchWindowByParam("showSaleOrderWindowCreator",{});
	},
	searchByCode : function() {
		var value = this.joinPanel.getForm().findField('_codeOfSeller').getValue();
		this.store.baseParams = {codeOfSeller : value};
		this.store.load();
	},
	queryByComplex : function() {
		this.advancedQueryPanel.getForm().reset();
		this.status_cmb.clearValue();
		this.openWindow.show();
	},
	newSaleOrder : function() {
		this.creator.app.getDesktop().launchWindowByParam("addOutSaleOrderWindowCreator",{});
	}
	
});

Uec3.ShowSaleDetailsWindowCreator = Ext.extend(Ext.app.Module, {
	id : 'showSaleDetailsWindowCreator',
	type : 'common/notepad',

	createWindow : function() {
		var desktop = this.app.getDesktop();
				var win = desktop.getWindow('showSaleDetailsWindow');
				if (win) {
					win.toFront();
				} else {				
					win = desktop.createWindow({
								id : 'showSaleDetailsWindow',
								title : '浏览销售订单明细',
								width : 1010,
								height : 500,
								iconCls : 'acc-icon',
								animCollapse : true,
								constrainHeader : true,
								maximizable : true,
								taskbuttonTooltip : "浏览销售订单明细",
								layout : "fit",
								items : []
							}, Uec3.ShowSaleDetailsWindow);
							
					win.creator = this;
					win.versionCode = ++(PurchaseCount.count);
					win.initPanel();
				}
	}
});